/**
 * 安全地获取嵌套对象的值
 * @param obj 对象
 * @param path 键值数组
 * @param fallback 获取失败的返回值
 */
export function getVal(obj, path, fallback) {
  try {
    let i
    const len = path.length
    for (i = 0; typeof obj === 'object' && i < len; ++i) {
      obj = obj[path[i]]
    }
    return obj || fallback
  } catch (e) {
    return fallback
  }
}

export function getValDot(obj, strPath, fallback) {
  const path = strPath.split('.')
  return getVal(obj, path, fallback)
}
